<--- %%NOBANNER%% --> words.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Count how many words in the string;                                |
| Note: the return value is the word counts; you can assign the value|
|       to another data step variable or a macro variable;           |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
| string: a string in which number of words need to be counted;      |
| dlm: the delimiter to separated words, default is an empty space;  |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %words(this is a| test, dlm=%quote(|));                   |
| Usage:   %words(string,dlm=%quote( ));                             |
\-------------------<-- End of Files Created-->---------------------*/
%macro words(string,dlm=%quote( ));
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 11:03pm;                |
| Modified: 11-2-2001 8:56pm;                 |
| Purpose:  Calculate the number of words in a|
|           string;                           |
\--------------------------------------------*/
%local _wcount_ word;
%let _wcount_=0; 
%do %while(%length(%nrbquote(%scan(%nrbquote(&string), %eval(&_wcount_+1), %nrbquote(&dlm)))));
   %let _wcount_=%eval(&_wcount_+1);
   %let word=%nrbquote(%qscan(%nrbquote(&string), &_wcount_, %nrbquote(&dlm)));
%end;&_wcount_
%put;
%mend words;